package com.cdream.basic.algorithm.leetcode.stack;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author cdream
 * @date 6/26/21
 */
public class Question199 {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        LinkedList<TreeNode> list = new LinkedList<>();
        list.add(root);
        while (root!=null && !list.isEmpty()){
            int size = list.size();
            for (int i = 0; i < size; i++) {
                TreeNode treeNode = list.poll();
                if (treeNode.left!=null){
                    list.offer(treeNode.left);
                }
                if (treeNode.right!=null){
                    list.offer(treeNode.right);
                }
                if (i==size-1){
                    res.add(treeNode.val);
                };


            }
        }
        return res;
    }

}
